package src.leetcode.everyday.mid_1705;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author liyizzz
 * @description 吃苹果的最大数目
 * @date 2024/12/24 14:26
 */
public class eatenApples {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.eatenApples(new int[]{1,2,3,5,2}, new int[]{3,2,1,4,2}));
    }
}

class Solution {
    public int eatenApples(int[] apples, int[] days) {
        int ans = 0;
        int n = days.length;
        PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
        for (int i = 0; i < n || !pq.isEmpty(); i++) {
            if(i < n) {
                pq.add(new int[]{days[i] + i - 1, apples[i]});
            }
            while (!pq.isEmpty() && (pq.peek()[0] < i || pq.peek()[1] <= 0)) {
                pq.poll();
            }
            if (!pq.isEmpty()) {
                pq.peek()[1]--;
                ans++;
            }
        }
        return ans;
    }
}